package com.yanqu.road.server.gameplayer.module.vehicle;

import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.config.vehicle.VehicleSoulBlockInfo;
import com.yanqu.road.entity.config.vehicle.VehicleSoulQualityInfo;
import com.yanqu.road.entity.config.vehicle.VehicleSoulShapeInfo;
import com.yanqu.road.entity.enums.*;
import com.yanqu.road.entity.log.vehiclesoul.*;
import com.yanqu.road.entity.player.UserVehicle;
import com.yanqu.road.entity.player.UserVehicleSoul;
import com.yanqu.road.logic.bussiness.player.UserVehicleSoulBussiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.pb.VehiclePb;
import com.yanqu.road.logic.pb.VehicleSoulPb;
import com.yanqu.road.pb.vehicle.VehicleProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.player.AttributeModule;
import com.yanqu.road.server.gameplayer.module.player.BagModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.DataAnalyticsMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.config.VehicleSoulMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.manger.maidian.DefaultEventTraceAction;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

//兽魂系统
public class VehicleSoulModule extends GeneralModule {


    private AtomicInteger soulIdIndex = new AtomicInteger(1);

    private Map<Integer, UserVehicleSoul> userVehicleSoulMap = new ConcurrentHashMap<>();

    private Map<String,Map<Integer,UserVehicleSoul>> equipMap = new ConcurrentHashMap<>();


    public VehicleSoulModule(GamePlayer player) {
        super(player);
    }


    //获取放了鸡块石头了
    private int getVehicleSoulPutNum(String vehicleId){
        if(!equipMap.containsKey(vehicleId)){
            return 0;
        }
        Map<Integer,UserVehicleSoul> soulMap = equipMap.get(vehicleId);
        return soulMap.size();
    }

    /**
     * 把这个座驾的所有格填充了的返回
     * @param vehicleId
     * @param soulId 要操作的那个石头要过滤掉
     * @return
     */
    private List<Integer> getVehicleSoulBoxFull(String vehicleId,int soulId){
        List<Integer> resList = new ArrayList<>();
        if(!equipMap.containsKey(vehicleId)){
            return resList;
        }
        Map<Integer,UserVehicleSoul> soulMap = equipMap.get(vehicleId);
        for(UserVehicleSoul userVehicleSoul : soulMap.values()){
            if(userVehicleSoul.getSoulId() == soulId)continue;
            VehicleSoulShapeInfo shapeInfo = VehicleSoulMgr.getShapeInfo(userVehicleSoul.getConfigId());
            List<Integer> posList = shapeInfo.getPosList();
            for(int posId : posList){
                int ppp = posId + userVehicleSoul.getEquipPos();
                if(resList.contains(ppp)){
                    log.error("兽魂炸裂了，有重复装备的点{}",ppp);
                    continue;
                }
                resList.add(ppp);
            }
        }
        return resList;
    }

    public UserVehicleSoul getUserSoul(int soulId){
        if(!userVehicleSoulMap.containsKey(soulId)){
            return null;
        }
        UserVehicleSoul userVehicleSoul = userVehicleSoulMap.get(soulId);
        if(userVehicleSoul.isDelete()){
            return null;
        }
        return userVehicleSoul;
    }

    public void conposeSoulTest(){
        VehicleSoulShapeInfo shapeInfo = VehicleSoulMgr.getShapeInfo(303);
        VehicleSoulQualityInfo qualityInfo = VehicleSoulMgr.getQualityInfo(shapeInfo.getQuality());
        VehicleSoulQualityInfo nextQualityInfo = VehicleSoulMgr.getQualityInfo(shapeInfo.getQuality() + 1);
        int times = 10000;
        int count3 = 0;
        int count4 = 0;
        int count3sameColor = 0;
        int count4sameColor = 0;
        for(int i = 0;i < times;i++){
            UserVehicleSoul userVehicleSoul = null;
            //随机下有没有升级
            if(nextQualityInfo != null){
                if(VehicleSoulMgr.isComposeUp(qualityInfo.getUpWeight())){
                    userVehicleSoul = makeOneSoul(null,nextQualityInfo.getQuality(),false);
                    count4++;
                    if(VehicleSoulMgr.isTheSameColor(userVehicleSoul.getOccList())){
                        count4sameColor++;
                    }
                }
            }
            if(userVehicleSoul == null){
                userVehicleSoul = makeOneSoul(null,qualityInfo.getQuality(),false);
                count3++;
                if(VehicleSoulMgr.isTheSameColor(userVehicleSoul.getOccList())){
                    count3sameColor++;
                }
            }
        }
        log.error("合成{}次,三星{}，三星全色{}，四星{},四星全色{}",times,count3,count3sameColor,count4,count4sameColor);
    }

    public void conposeSoulTest2(){
        Map<Integer,List<UserVehicleSoul>> userVehicleMap = new ConcurrentHashMap<>();
        userVehicleMap.put(1,new ArrayList<>());
        userVehicleMap.put(2,new ArrayList<>());
        userVehicleMap.put(3,new ArrayList<>());
        userVehicleMap.put(4,new ArrayList<>());
        VehicleSoulShapeInfo shapeInfo = VehicleSoulMgr.getShapeInfo(101);
        VehicleSoulQualityInfo qualityInfo = VehicleSoulMgr.getQualityInfo(shapeInfo.getQuality());
        VehicleSoulQualityInfo nextQualityInfo = VehicleSoulMgr.getQualityInfo(shapeInfo.getQuality() + 1);
        int times = 872083;
        int count4sameColor = 0;
        for(int i = 0;i < times;i++){
            UserVehicleSoul userVehicleSoul = null;
            //随机下有没有升级
            if(nextQualityInfo != null){
                if(VehicleSoulMgr.isComposeUp(qualityInfo.getUpWeight())){
                    userVehicleSoul = makeOneSoul(null,nextQualityInfo.getQuality(),false);
                    userVehicleMap.get(nextQualityInfo.getQuality()).add(userVehicleSoul);
                }
            }
            if(userVehicleSoul == null){
                userVehicleSoul = makeOneSoul(null,qualityInfo.getQuality(),false);
                userVehicleMap.get(qualityInfo.getQuality()).add(userVehicleSoul);
            }
        }
        for(List<UserVehicleSoul> userVehicleSoulList : userVehicleMap.values()){
            while (userVehicleSoulList.size() > 1){
                UserVehicleSoul userVehicleSoul = userVehicleSoulList.get(0);
                userVehicleSoulList.remove(0);
                userVehicleSoulList.remove(0);
                shapeInfo = VehicleSoulMgr.getShapeInfo(userVehicleSoul.getConfigId());
                qualityInfo = VehicleSoulMgr.getQualityInfo(shapeInfo.getQuality());
                nextQualityInfo = VehicleSoulMgr.getQualityInfo(shapeInfo.getQuality() + 1);
                UserVehicleSoul composeVehicleSoul = null;
                //随机下有没有升级
                if(nextQualityInfo != null){
                    if(VehicleSoulMgr.isComposeUp(qualityInfo.getUpWeight())){
                        composeVehicleSoul = makeOneSoul(null,nextQualityInfo.getQuality(),false);
                        if(nextQualityInfo.getQuality() == 4 && VehicleSoulMgr.isTheSameColor(composeVehicleSoul.getOccList())){
                            count4sameColor++;
                        }else{
                            userVehicleMap.get(nextQualityInfo.getQuality()).add(composeVehicleSoul);
                        }
                    }
                }
                if(composeVehicleSoul == null){
                    composeVehicleSoul = makeOneSoul(null,qualityInfo.getQuality(),false);
                    if(qualityInfo.getQuality() == 4 && VehicleSoulMgr.isTheSameColor(composeVehicleSoul.getOccList())){
                        count4sameColor++;
                    }else{
                        userVehicleMap.get(qualityInfo.getQuality()).add(composeVehicleSoul);
                    }
                }
            }
        }
        log.error("{}个1星,最终{}四星全色",times,count4sameColor);
    }

    //合成魂石
    public int composeSoul(List<Integer> soulIdList,int lockType){
//        conposeSoulTest2();
        //只能两个合成一个
        if(soulIdList.size() != 2){
            return GameErrorCode.E_VEHICLE_SOUL_TWO_TO_ONE;
        }

        UserVehicleSoul soul1 = getUserSoul(soulIdList.get(0));
        UserVehicleSoul soul2 = getUserSoul(soulIdList.get(1));

        if(soul1 == null || soul2 == null){
            return GameErrorCode.E_VEHICLE_SOUL_NOT_FOUND;
        }
        VehicleSoulShapeInfo shapeInfo1 = VehicleSoulMgr.getShapeInfo(soul1.getConfigId());
        VehicleSoulShapeInfo shapeInfo2 = VehicleSoulMgr.getShapeInfo(soul2.getConfigId());

        if(shapeInfo1.getQuality() != shapeInfo2.getQuality()){
            return GameErrorCode.E_VEHICLE_SOUL_QUALITY_NOT_SAME;
        }

        //只有4品质的才能锁定东西 1锁定颜色 2锁定形状
        if(shapeInfo1.getQuality() != 4 && lockType != 0){
            return GameErrorCode.E_VEHICLE_SOUL_QUALITY_NOT_LOCK;
        }


        if(!StringUtils.isNullOrEmpty(soul1.getVehicleId()) || !StringUtils.isNullOrEmpty(soul2.getVehicleId())){
            return GameErrorCode.E_VEHICLE_SOUL_IS_EQUIP;
        }

        if(soul1.isLock() || soul2.isLock()){
            return GameErrorCode.E_VEHICLE_SOUL_IS_LOCK;
        }

        soul1.setDelete(true);
        soul2.setDelete(true);
        AutoLogMgr.add(new LogVehicleSoulGet(getUserId(), 0, soul1.getSoulId(), soul1.getConfigId(),
                soul1.getOccList().size(),
                StringUtils.listToString(soul1.getOccList(), "|"),
                VehicleSoulMgr.getSoulAllQaStr(soul1), 3));
        AutoLogMgr.add(new LogVehicleSoulGet(getUserId(), 0, soul2.getSoulId(), soul2.getConfigId(),
                soul2.getOccList().size(),
                StringUtils.listToString(soul2.getOccList(), "|"),
                VehicleSoulMgr.getSoulAllQaStr(soul2), 3));
//        userVehicleSoulMap.remove(soul1.getSoulId());
//        userVehicleSoulMap.remove(soul2.getSoulId());

        VehicleSoulShapeInfo shapeInfo = VehicleSoulMgr.getShapeInfo(soul1.getConfigId());
        VehicleSoulQualityInfo qualityInfo = VehicleSoulMgr.getQualityInfo(shapeInfo.getQuality());
        VehicleSoulQualityInfo nextQualityInfo = VehicleSoulMgr.getQualityInfo(shapeInfo.getQuality() + 1);
        UserVehicleSoul userVehicleSoul = null;
        String reward1 = "";
        String reward2 = "";
        //随机下有没有升级
        if(nextQualityInfo != null){
            if(VehicleSoulMgr.isComposeUp(qualityInfo.getUpWeight())){
                userVehicleSoul = makeOneSoul(null,nextQualityInfo.getQuality(),true);
                //合成同色和非同色的奖励不一样
                if(!VehicleSoulMgr.isTheSameColor(userVehicleSoul.getOccList())){
                    reward1 = nextQualityInfo.getReward1();
                }else{
                    reward2 = nextQualityInfo.getReward2();
                }
            }
        }
        //没有升级，随机原先等级的
        if(userVehicleSoul == null){
            userVehicleSoul = makeOneSoul(null,qualityInfo.getQuality(),true);
            if(!VehicleSoulMgr.isTheSameColor(userVehicleSoul.getOccList())){
                reward1 = qualityInfo.getReward1();
            }else{
                reward2 = qualityInfo.getReward2();
            }
        }

        long attributeCompose = player.getModule(AttributeModule.class).getAttribute(ePlayerAttrType.SoulWuShuang);
        if (shapeInfo1.getQuality() == 4 && VehicleSoulMgr.VEHICLES_PEERLESS_GUARANTEES - attributeCompose <= 1) {
            if (lockType == 2) {
                //2锁定形状
                userVehicleSoul.setConfigId(soul1.getConfigId());
            }
            // 随机颜色
            VehicleSoulShapeInfo shapeInfoTemp = VehicleSoulMgr.getShapeInfo(userVehicleSoul.getConfigId());
            userVehicleSoul.setOccList(
                    VehicleSoulMgr.randomOneShapeOccList(
                            VehicleSoulMgr.getThreadSafeRandom().next(eOccupationType.Scholar.getValue(), eOccupationType.Knight.getValue() + 1),
                            shapeInfoTemp.getNum(), shapeInfoTemp.getQuality() == 4));
            player.getModule(AttributeModule.class).setAttribute(ePlayerAttrType.SoulWuShuang, 0);
        } else {
            //锁定颜色 第一个的颜色
            if(lockType == 1){
                userVehicleSoul.setOccList(new ArrayList<>(soul1.getOccList()));
            }else if(lockType == 2){
                //2锁定形状
                userVehicleSoul.setConfigId(soul1.getConfigId());
            }
            if (shapeInfo1.getQuality() == 4) {
                player.getModule(AttributeModule.class).setAttribute(ePlayerAttrType.SoulWuShuang, attributeCompose + 1);
            }
        }

        if(!StringUtils.isNullOrEmpty(reward1)){
            player.getModule(CurrencyModule.class).addCurrency(reward1,eLogMoneyType.VehicleSoul,eLogMoneyType.VehicleSoulComposeReward1);
        }else{
            player.getModule(CurrencyModule.class).addCurrency(reward2,eLogMoneyType.VehicleSoul,eLogMoneyType.VehicleSoulComposeReward2);
        }
        VehicleProto.VehicleSoulComposeRespMsg.Builder repsMsg = VehicleProto.VehicleSoulComposeRespMsg.newBuilder();
        repsMsg.setRet(0);
        if(!StringUtils.isNullOrEmpty(reward1)){
            repsMsg.setReward(reward1);
        }else{
            repsMsg.setReward(reward2);
        }
        if(userVehicleSoul != null){
            repsMsg.setSoul(VehicleSoulPb.parseVehicleSoulTempMsg(userVehicleSoul));
        }
        repsMsg.setFourLvSoulComposeRemainNum((int) (VehicleSoulMgr.VEHICLES_PEERLESS_GUARANTEES - player.getModule(AttributeModule.class).getAttribute(ePlayerAttrType.SoulWuShuang)));
        player.sendPacket(Protocol.U_VSOUL_COMPOSE, repsMsg);
        List<UserVehicleSoul> syncList = new ArrayList<>();
        syncList.add(userVehicleSoul);
        syncVehicleSoulData(syncList,false);


        AutoLogMgr.add(new LogVehicleSoulCompose(getUserId(), shapeInfo1.getQuality(),
                lockType, 1, 1, userVehicleSoul.getOccList().size() + ":1"));
        return 0;
    }

    //转换魂石
    public int turnSoul(int soulId, int occ) {
        UserVehicleSoul soulSrc = getUserSoul(soulId);
        if (soulSrc == null) {
            return GameErrorCode.E_VEHICLE_SOUL_NOT_FOUND;
        }
        if (!isSameColor(soulSrc.getOccList())) {
            return GameErrorCode.E_VEHICLE_SOUL_COLOR_ERROR;
        }

        VehicleSoulShapeInfo shapeInfo = VehicleSoulMgr.getShapeInfo(soulSrc.getConfigId());
        if (shapeInfo == null || shapeInfo.getQuality() < 3) {
            return GameErrorCode.E_VEHICLE_SOUL_QUALITY_ERROR;
        }

        int vehiclesTurnCostItem = VehicleSoulMgr.getVehiclesTurnCostItem(shapeInfo.getQuality());
        if (vehiclesTurnCostItem == 0) {
            return GameErrorCode.E_VEHICLE_TURN_NOT_FOUND;
        }
        Property cost = new Property(vehiclesTurnCostItem, 1);
        if (!player.getModule(CurrencyModule.class).removeCurrency(cost, eLogMoneyType.VehicleSoul, eLogMoneyType.VehicleSoulTurnCost)) {
            return GameErrorCode.E_VEHICLE_TURN_NOT_ENOUGH;
        }

        soulSrc.setOccList(VehicleSoulMgr.randomOneShapeOccList(occ, shapeInfo.getNum(), shapeInfo.getQuality() == 4));

        VehicleProto.VehicleSoulTurnRespMsg.Builder repsMsg = VehicleProto.VehicleSoulTurnRespMsg.newBuilder();
        repsMsg.setRet(0);
        repsMsg.setSoul(VehicleSoulPb.parseVehicleSoulTempMsg(soulSrc));
        player.sendPacket(Protocol.U_VSOUL_TURN, repsMsg);
        List<UserVehicleSoul> syncList = new ArrayList<>();
        syncList.add(soulSrc);
        syncVehicleSoulData(syncList,false);
        return 0;
    }

    private boolean isSameColor(List<Integer> occList){
        int occ = 0;
        for(int occId : occList){
            if(occ == 0){
                occ = occId;
            }
            if(occ != occId){
                return false;
            }
        }
        return true;
    }

    /**
     *
     * @param quality
     * @param num
     * @param lockColor 锁定同色不合成
     * @return
     */
    private List<UserVehicleSoul> getComposeList(int quality,int num,boolean lockColor){
        List<UserVehicleSoul> list = new ArrayList<>();
        int count = 0;
        for(UserVehicleSoul userVehicleSoul : userVehicleSoulMap.values()){
            //没有删除和锁定
            if(!userVehicleSoul.isDelete() && !userVehicleSoul.isLock()){
                //没有装备
                if(StringUtils.isNullOrEmpty(userVehicleSoul.getVehicleId())){
                    VehicleSoulShapeInfo shapeInfo = VehicleSoulMgr.getShapeInfo(userVehicleSoul.getConfigId());
                    //品质对
                    if(shapeInfo.getQuality() == quality){
                        //有没有锁颜色
                        if(!lockColor || !isSameColor(userVehicleSoul.getOccList())){
                            list.add(userVehicleSoul);
                            count++;
                            if(count >= num){
                                break;
                            }
                        }
                    }
                }
            }
        }

        return list;
    }

    //合成魂石一键
    public int composeSoulOneKey(int quality,int num,boolean lockColor,boolean isLoop){
        if(!isOnekeyCompose()){
            return GameErrorCode.E_VEHICLE_SOUL_ONEKEY_COMPOSE;
        }
        if(quality > 3){
            return GameErrorCode.E_VEHICLE_SOUL_ONEKEY_COMPOSE_QUALITY;
        }
        List<UserVehicleSoul> composeList = getComposeList(quality,num,lockColor);
        if(composeList.size() != num){
            return GameErrorCode.E_VEHICLE_SOUL_ONEKEY_COMPOSE_NUM;
        }
        //合成完后要删除的id
        List<UserVehicleSoul> delList = new ArrayList<>();
        //合成成下一级的
        List<UserVehicleSoul> nextList = new ArrayList<>();
        Property rewardPro = new Property();
        //一直合下去，知道没有1级的可以合
        int composeTimes = 0;
        while(composeList.size() > 1){
            composeTimes++;
            UserVehicleSoul soul1 = composeList.get(0);
            UserVehicleSoul soul2 = composeList.get(1);

            VehicleSoulShapeInfo shapeInfo = VehicleSoulMgr.getShapeInfo(soul1.getConfigId());
            VehicleSoulQualityInfo qualityInfo = VehicleSoulMgr.getQualityInfo(shapeInfo.getQuality());
            VehicleSoulQualityInfo nextQualityInfo = VehicleSoulMgr.getQualityInfo(shapeInfo.getQuality() + 1);
            UserVehicleSoul userVehicleSoul = null;
            String reward = "";
            //随机下有没有升级
            if(nextQualityInfo != null){
                if(VehicleSoulMgr.isComposeUp(qualityInfo.getUpWeight())){
                    userVehicleSoul = makeOneSoul(null,nextQualityInfo.getQuality(),false);
                    //合成同色和非同色的奖励不一样
                    if(!VehicleSoulMgr.isTheSameColor(userVehicleSoul.getOccList())){
                        reward = nextQualityInfo.getReward1();
                    }else{
                        reward = nextQualityInfo.getReward2();
                    }
                }
            }
            //没有升级，随机原先等级的
            if(userVehicleSoul == null){
                userVehicleSoul = makeOneSoul(null,qualityInfo.getQuality(),false);
                if(!VehicleSoulMgr.isTheSameColor(userVehicleSoul.getOccList())){
                    reward = qualityInfo.getReward1();
                }else{
                    reward = qualityInfo.getReward2();
                }
            }
            rewardPro.addProperty(PropertyHelper.parseStringToProperty(reward));
            composeList.remove(soul1);
            composeList.remove(soul2);
            delList.add(soul1);
            delList.add(soul2);
            VehicleSoulShapeInfo shapeInfoCompose = VehicleSoulMgr.getShapeInfo(userVehicleSoul.getConfigId());
            if(isLoop){
                //要么升级了，或者只有两个合成一个下一次肯定喝不了了,或者锁同色，同色也发下去
                if((VehicleSoulMgr.isTheSameColor(userVehicleSoul.getOccList()) && lockColor) ||
                    shapeInfoCompose.getQuality() == quality + 1 || composeList.size() == 0){
                    nextList.add(userVehicleSoul);
                }else{
                    userVehicleSoul.setTemp(true);
                    composeList.add(userVehicleSoul);
                }
            }else{
                nextList.add(userVehicleSoul);
            }
        }

        List<Integer> delIdList = new ArrayList<>();
        for(UserVehicleSoul delSoul : delList){
            if(!delSoul.isTemp()){
                delIdList.add(delSoul.getSoulId());
            }
            delSoul.setDelete(true);
            AutoLogMgr.add(new LogVehicleSoulGet(getUserId(), 0, delSoul.getSoulId(), delSoul.getConfigId(),
                    delSoul.getOccList().size(),
                    StringUtils.listToString(delSoul.getOccList(), "|"),
                    VehicleSoulMgr.getSoulAllQaStr(delSoul), 3));
//            userVehicleSoulMap.remove(delSoul.getSoulId());
        }

        for(UserVehicleSoul addSoul : nextList){
            userVehicleSoulMap.put(addSoul.getSoulId(),addSoul);
            AutoLogMgr.add(new LogVehicleSoulGet(getUserId(), 1, addSoul.getSoulId(), addSoul.getConfigId(),
                    addSoul.getOccList().size(),
                    StringUtils.listToString(addSoul.getOccList(), "|"),
                    VehicleSoulMgr.getSoulAllQaStr(addSoul), 2));
        }

        if(!StringUtils.isNullOrEmpty(PropertyHelper.parsePropertyToString(rewardPro))){
            player.getModule(CurrencyModule.class).addCurrency(rewardPro,eLogMoneyType.VehicleSoul,eLogMoneyType.VehicleSoulComposeReward1);
        }

        VehicleProto.VehicleSoulOneKeyComposeRespMsg.Builder repsMsg = VehicleProto.VehicleSoulOneKeyComposeRespMsg.newBuilder();
        repsMsg.setRet(0);
        repsMsg.setReward(PropertyHelper.parsePropertyToString(rewardPro));
        List<UserVehicleSoul> syncList = new ArrayList<>();
        for(UserVehicleSoul addSoul : nextList){
            repsMsg.addSoul(VehicleSoulPb.parseVehicleSoulTempMsg(addSoul));
            syncList.add(addSoul);
        }
        repsMsg.addAllSoulIds(delIdList);
        repsMsg.setComposeTimes(composeTimes);
        player.sendPacket(Protocol.U_VSOUL_COMPOSE_ONEKEY, repsMsg);
//        log.error("删除的魂石id是{}",StringUtils.listToString(delIdList,","));
        syncVehicleSoulData(syncList,false);

        AutoLogMgr.add(new LogVehicleSoulCompose(getUserId(), quality,
                lockColor ? 2 : 1, isLoop ? 3 : 2, composeTimes, VehicleSoulMgr.getSoulQaStr(nextList)));
        return 0;
    }


    //洗词条
    public int washSoul(int soulId,int index){
        UserVehicleSoul userVehicleSoul = getUserSoul(soulId);
        if(userVehicleSoul == null){
            return GameErrorCode.E_VEHICLE_SOUL_NOT_FOUND;
        }
        Map<Integer, List<Integer>> entryMap = userVehicleSoul.getEntryMap();
        if(!entryMap.containsKey(index)){
            return GameErrorCode.E_VEHICLE_SOUL_ENTRY_ILLEGAL;
        }
        List<Integer> infoList = entryMap.get(index);
        String cost = VehicleSoulMgr.getWashCost(infoList.get(3));
        if(!player.getModule(CurrencyModule.class).currencyIsEnough(PropertyHelper.parseStringToProperty(cost))){
            return GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
        }
        player.getModule(CurrencyModule.class).removeCurrency(PropertyHelper.parseStringToProperty(cost),eLogMoneyType.VehicleSoul,eLogMoneyType.VehicleSoulWashCost);
        List<Integer> nEntryList = VehicleSoulMgr.randomOneShapeEntry(infoList);
        entryMap.put(index,nEntryList);
        userVehicleSoul.setEntryMap(entryMap);

        VehicleProto.VehicleSoulWashRespMsg.Builder respMsg = VehicleProto.VehicleSoulWashRespMsg.newBuilder();
        respMsg.setRet(0);
        respMsg.setOcc(nEntryList.get(4));
        respMsg.setValue(nEntryList.get(5));
        player.sendPacket(Protocol.U_VSOUL_WASH, respMsg);

        AutoLogMgr.add(new LogVehicleSoulWash(getUserId(), soulId, userVehicleSoul.getConfigId(), index,
                infoList.get(1) + ":" + infoList.get(2), nEntryList.get(4) + ":" + nEntryList.get(5), 1, cost
        ));
        return 0;
    }

    //确认洗练
    public int washSoulSure(int soulId,int index){
        UserVehicleSoul userVehicleSoul = getUserSoul(soulId);
        if(userVehicleSoul == null){
            return GameErrorCode.E_VEHICLE_SOUL_NOT_FOUND;
        }

        Map<Integer, List<Integer>> entryMap = userVehicleSoul.getEntryMap();
        if(!entryMap.containsKey(index)){
            return GameErrorCode.E_VEHICLE_SOUL_ENTRY_ILLEGAL;
        }

        List<Integer> infoList = entryMap.get(index);
        if(infoList.size() != 6){
            return GameErrorCode.E_VEHICLE_SOUL_ENTRY_ILLEGAL;
        }
        List<String> oldLogList = null;
        if(!"".equals(userVehicleSoul.getVehicleId())){
            UserVehicle userVehicle = player.getModule(VehicleModule.class).getUserVehicle(userVehicleSoul.getVehicleId());
            oldLogList = VehicleSoulMgr.getSoulAddStrLog(player,userVehicle);
        }
        List<Integer> newInfoList = new ArrayList<>();
        newInfoList.add(infoList.get(0));
        newInfoList.add(infoList.get(4));
        newInfoList.add(infoList.get(5));
        newInfoList.add(infoList.get(3));
        entryMap.put(index,newInfoList);
        userVehicleSoul.setEntryMap(entryMap);

        VehicleProto.VehicleSoulWashSureRespMsg.Builder respMsg = VehicleProto.VehicleSoulWashSureRespMsg.newBuilder();
        respMsg.setRet(0);
        player.sendPacket(Protocol.U_VSOUL_WASH_SURE, respMsg);
        if(!StringUtils.isNullOrEmpty(userVehicleSoul.getVehicleId())){
            UserVehicle userVehicle = player.getModule(VehicleModule.class).getUserVehicle(userVehicleSoul.getVehicleId());
            player.getModule(VehicleModule.class).onVehicleChange(userVehicle);
        }
        AutoLogMgr.add(new LogVehicleSoulWash(getUserId(), soulId, userVehicleSoul.getConfigId(), index,
                infoList.get(1) + ":" + infoList.get(2), newInfoList.get(1) + ":" + newInfoList.get(2), 2, ""
        ));
        if(!"".equals(userVehicleSoul.getVehicleId()) && oldLogList != null && oldLogList.size() == 2){
            UserVehicle userVehicle = player.getModule(VehicleModule.class).getUserVehicle(userVehicleSoul.getVehicleId());
            List<String> newLogList = VehicleSoulMgr.getSoulAddStrLog(player,userVehicle);
            AutoLogMgr.add(new LogVehicleSoulEquip(
                    player.getUserId(),
                    soulId,
                    userVehicle.getTemplateId(),
                    userVehicleSoul.getVehicleId(),
                    userVehicle.getPatronsId(),
                    3,
                    oldLogList.get(0),
                    newLogList.get(0),
                    oldLogList.get(1),
                    newLogList.get(1),
                    0,
                    userVehicle.getSoulBlockList().size()
            ));
        }
        return 0;
    }

    public int washSoulCancel(int soulId,int index){
        UserVehicleSoul userVehicleSoul = getUserSoul(soulId);
        if(userVehicleSoul == null){
            return GameErrorCode.E_VEHICLE_SOUL_NOT_FOUND;
        }

        Map<Integer, List<Integer>> entryMap = userVehicleSoul.getEntryMap();
        if(!entryMap.containsKey(index)){
            return GameErrorCode.E_VEHICLE_SOUL_ENTRY_ILLEGAL;
        }
        List<Integer> infoList = entryMap.get(index);
        if(infoList.size() != 6){
            return GameErrorCode.E_VEHICLE_SOUL_ENTRY_ILLEGAL;
        }
        String logRes = infoList.get(4) + ":" + infoList.get(5);
        infoList.remove(4);
        infoList.remove(4);
        userVehicleSoul.setEntryMap(entryMap);

        VehicleProto.VehicleSoulWashCancelRespMsg.Builder respMsg = VehicleProto.VehicleSoulWashCancelRespMsg.newBuilder();
        respMsg.setRet(0);
        player.sendPacket(Protocol.U_VSOUL_WASH_CANCEL, respMsg);

        AutoLogMgr.add(new LogVehicleSoulWash(getUserId(), soulId, userVehicleSoul.getConfigId(), index,
                infoList.get(1) + ":" + infoList.get(2), logRes, 3, ""
        ));
        return 0;
    }

    //珍兽开魂孔
    public int openSoulBlock(String vehicleId,int pos){
        UserVehicle userVehicle = player.getModule(VehicleModule.class).getUserVehicle(vehicleId);
        List<Integer> soulBlockList = userVehicle.getSoulBlockList();
        if(soulBlockList.contains(pos)){
            return GameErrorCode.E_VEHICLE_SOUL_BLOCK_IS_OPEN;
        }
        if(pos < 0){
            return GameErrorCode.E_VEHICLE_SOUL_BLOCK_ILLEGAL;
        }
        if(pos / 10 >= GameConfig.VEHICLES_SOUL_MAP_LENGTH){
            return GameErrorCode.E_VEHICLE_SOUL_BLOCK_ILLEGAL;
        }
        if(pos % 10 >= GameConfig.VEHICLES_SOUL_MAP_LENGTH){
            return GameErrorCode.E_VEHICLE_SOUL_BLOCK_ILLEGAL;
        }
        VehicleSoulBlockInfo soulBlockInfo = VehicleSoulMgr.getSoulBlockInfo(soulBlockList.size() + 1);
        if(soulBlockInfo == null){
            return GameErrorCode.E_VEHICLE_SOUL_BLOCK_ILLEGAL;
        }
        if(!"0".equals(soulBlockInfo.getUpCost())){
            if(!player.getModule(CurrencyModule.class).currencyIsEnough(PropertyHelper.parseStringToProperty(soulBlockInfo.getUpCost()))){
                //道具不足触发现金礼包
                player.notifyListener(eGamePlayerEventType.RechargeVehicleSoulBlock.getValue(), 0);
                return  GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            }
            player.getModule(CurrencyModule.class).removeCurrency(PropertyHelper.parseStringToProperty(soulBlockInfo.getUpCost()), eLogMoneyType.Vehicle,eLogMoneyType.VehicleSoulBlockOpen);
        }
        soulBlockList.add(pos);
        userVehicle.setSoulBlockList(soulBlockList);
        //开孔会导致满盘没了
        player.getModule(VehicleModule.class).onVehicleChange(userVehicle);

        //消耗是110277
        Property costProperty = new Property();
        if(!"0".equals(soulBlockInfo.getUpCost())){
            costProperty = PropertyHelper.parseStringToProperty(soulBlockInfo.getUpCost());
        }
        AutoLogMgr.add(new LogVehicleSoulOpen(player.getUserId(),
                userVehicle.getTemplateId(),
                userVehicle.getVehicleUid(),
                pos,
                costProperty.getCountByGoodsId(GameConfig.VEHICLES_SOUL_BLOCK_OPEN_ITEM_ID).intValue(),
                (int) (player.getModule(BagModule.class).getUserBagItemCount(GameConfig.VEHICLES_SOUL_BLOCK_OPEN_ITEM_ID)),
                userVehicle.getSoulBlockList().size()
        ));
        return 0;
    }

    //锁定魂石
    public int lockSoul(int soulId,boolean isLock){
        UserVehicleSoul userVehicleSoul = getUserSoul(soulId);
        if(userVehicleSoul == null){
            return GameErrorCode.E_VEHICLE_SOUL_NOT_FOUND;
        }
        userVehicleSoul.setLock(isLock);
        VehicleProto.VehicleSoulLockRespMsg.Builder respMsg = VehicleProto.VehicleSoulLockRespMsg.newBuilder();
        respMsg.setRet(0);
        respMsg.setIsLock(isLock);
        player.sendPacket(Protocol.U_VSOUL_LOCK, respMsg);
        return 0;
    }

    //分解和重置时候
    public void unloadSoul(String vehicleUid) {
        List<UserVehicleSoul> needSyncList = new ArrayList<>();
        if(equipMap.containsKey(vehicleUid)){
            for(UserVehicleSoul userVehicleSoul : equipMap.get(vehicleUid).values()){
                userVehicleSoul.setVehicleId("");
            }
            needSyncList.addAll(equipMap.get(vehicleUid).values());
            syncVehicleSoulData(needSyncList,false);
            equipMap.remove(vehicleUid);
        }
        UserVehicle userVehicle = player.getModule(VehicleModule.class).getUserVehicle(vehicleUid);
        player.getModule(VehicleModule.class).onVehicleChange(userVehicle);
    }

    //装备魂石
    public int equipSoul(int soulId,String vehicleId,int pos){
        UserVehicleSoul userVehicleSoul = getUserSoul(soulId);
        if(userVehicleSoul == null){
            return GameErrorCode.E_VEHICLE_SOUL_NOT_FOUND;
        }
        if(StringUtils.isNullOrEmpty(vehicleId) && StringUtils.isNullOrEmpty(userVehicleSoul.getVehicleId())){
            return GameErrorCode.E_VEHICLE_SOUL_NOT_EQUIP;
        }
        if(!StringUtils.isNullOrEmpty(vehicleId) && !StringUtils.isNullOrEmpty(userVehicleSoul.getVehicleId())){
            return GameErrorCode.E_VEHICLE_SOUL_IS_EQUIP;
        }
        String oldVehicleId = "";
        if(!"".equals(vehicleId)){
            oldVehicleId = vehicleId;
        }else{
            oldVehicleId = userVehicleSoul.getVehicleId();
        }
        UserVehicle oldVehicle = player.getModule(VehicleModule.class).getUserVehicle(oldVehicleId);
        List<String> oldLogList = VehicleSoulMgr.getSoulAddStrLog(player,oldVehicle);

        if(StringUtils.isNullOrEmpty(vehicleId)){
            userVehicleSoul.setVehicleId(vehicleId);
            userVehicleSoul.setEquipTime(0);
            userVehicleSoul.setEquipPos(0);
            removeCacheSoulToVehicle(oldVehicleId,userVehicleSoul);
            UserVehicle userVehicle = player.getModule(VehicleModule.class).getUserVehicle(oldVehicleId);
            player.getModule(VehicleModule.class).onVehicleChange(userVehicle);

            //赚钱加成和满盘加成
            List<String> newLogList = VehicleSoulMgr.getSoulAddStrLog(player,userVehicle);
            AutoLogMgr.add(new LogVehicleSoulEquip(
                    player.getUserId(),
                    soulId,
                    userVehicle.getTemplateId(),
                    oldVehicleId,
                    userVehicle.getPatronsId(),
                    2,
                    oldLogList.get(0),
                    newLogList.get(0),
                    oldLogList.get(1),
                    newLogList.get(1),
                    pos,
                    userVehicle.getSoulBlockList().size()
            ));
            return 0;
        }

        UserVehicle userVehicle = player.getModule(VehicleModule.class).getUserVehicle(vehicleId);
        VehicleSoulBlockInfo soulBlockInfo = VehicleSoulMgr.getSoulBlockInfo(userVehicle.getSoulBlockList().size());
        if(soulBlockInfo == null){
            return GameErrorCode.E_VEHICLE_SOUL_EQUIP_MAX;
        }
        if(getVehicleSoulPutNum(vehicleId) + 1 > soulBlockInfo.getSetSoulNum()){
            return GameErrorCode.E_VEHICLE_SOUL_EQUIP_MAX;
        }

        //只能是大于0的偏移
        if(pos < 0){
            return GameErrorCode.E_VEHICLE_SOUL_EQUIP_POS;
        }
        VehicleSoulShapeInfo shapeInfo = VehicleSoulMgr.getShapeInfo(userVehicleSoul.getConfigId());
        List<Integer> posList = getVehicleSoulBoxFull(vehicleId,soulId);
        //判断这些位置有没有和其他的块叠一起的
        for(int configPos : shapeInfo.getPosList()){
            if(posList.contains(configPos + pos)){
                return GameErrorCode.E_VEHICLE_SOUL_EQUIP_POS;
            }
        }

        //判断这些位置有没有跑出去，现在是按照10x10的大小，实际玩家的格子是5*5
        for(int configPos : shapeInfo.getPosList()){
            int blockPos = configPos + pos;
            if(blockPos / 10 >= GameConfig.VEHICLES_SOUL_MAP_LENGTH){
                return GameErrorCode.E_VEHICLE_SOUL_EQUIP_POS;
            }
            if(blockPos % 10 >= GameConfig.VEHICLES_SOUL_MAP_LENGTH){
                return GameErrorCode.E_VEHICLE_SOUL_EQUIP_POS;
            }
        }

        userVehicleSoul.setVehicleId(vehicleId);
        userVehicleSoul.setEquipTime(System.currentTimeMillis());
        userVehicleSoul.setEquipPos(pos);


        cacheSoulToVehicle(vehicleId,userVehicleSoul);

        player.getModule(VehicleModule.class).onVehicleChange(userVehicle);



        //赚钱加成和满盘加成
        List<String> newLogList = VehicleSoulMgr.getSoulAddStrLog(player,userVehicle);
        AutoLogMgr.add(new LogVehicleSoulEquip(
                player.getUserId(),
                soulId,
                userVehicle.getTemplateId(),
                vehicleId,
                userVehicle.getPatronsId(),
                1,
                oldLogList.get(0),
                newLogList.get(0),
                oldLogList.get(1),
                newLogList.get(1),
                pos,
                userVehicle.getSoulBlockList().size()
        ));
        return 0;
    }


    private void cacheSoulToVehicle(String vehicleId,UserVehicleSoul userVehicleSoul){
        synchronized (equipMap){
            if(!equipMap.containsKey(vehicleId)){
                equipMap.put(vehicleId,new ConcurrentHashMap<>());
            }
        }
        equipMap.get(vehicleId).put(userVehicleSoul.getSoulId(),userVehicleSoul);
    }

    private void removeCacheSoulToVehicle(String vehicleId,UserVehicleSoul userVehicleSoul){
        if(!equipMap.containsKey(vehicleId)){
            return;
        }
        equipMap.get(vehicleId).remove(userVehicleSoul.getSoulId());
    }

    //可以创吗，是不是达到最大的限制了
    public boolean canMakeOne(long count) {
        int allCount = 0;
        for(UserVehicleSoul userVehicleSoul : userVehicleSoulMap.values()){
            if(!userVehicleSoul.isDelete()){
                allCount++;
            }
        }
        if(count + allCount >= GameConfig.VEHICLES_SOUL_MAX){
            return false;
        }
        return true;
    }


    public void make5Soul() {
        if(!SystemOpenMgr.systemOpen(player, eSystemId.VehicleSoul.getValue())){
            return;
        }
        if(soulIdIndex.get() == 1){
            for(eOccupationType occupationType : eOccupationType.values()){
                VehicleSoulShapeInfo soulShapeInfo = VehicleSoulMgr.getQualityOneShapInfo();
                if(soulShapeInfo != null){
                    UserVehicleSoul userVehicleSoul = new UserVehicleSoul();
                    userVehicleSoul.setUserId(player.getUserId());
                    userVehicleSoul.setSoulId(soulIdIndex.getAndIncrement());
                    userVehicleSoul.setConfigId(soulShapeInfo.getId());
                    userVehicleSoul.setVehicleId("");
                    userVehicleSoul.setEquipTime(0);
                    userVehicleSoul.setEquipPos(0);
                    userVehicleSoul.setInsertOption();
                    //每个砖块的职业和副词条加资质
                    userVehicleSoul.setOccList(VehicleSoulMgr.randomOneShapeOccList(occupationType.getValue(),soulShapeInfo.getNum(),false));
                    userVehicleSoul.setEntryMap(VehicleSoulMgr.randomOneShapeEntryList(soulShapeInfo));

                    userVehicleSoulMap.put(userVehicleSoul.getSoulId(),userVehicleSoul);
                    List<UserVehicleSoul> syncList = new ArrayList<>();
                    syncList.add(userVehicleSoul);
                    syncVehicleSoulData(syncList, false);
                }
            }
        }

    }
    /**
     *
     * @param goodsInfo 道具开出来的
     * @param makeQuality 合成的
     * @param save  是不是要入库，一键合成就不入库了，后面再添加
     * @return
     */
    public UserVehicleSoul makeOneSoul(GoodsInfo goodsInfo,int makeQuality,boolean save){
        VehicleSoulShapeInfo soulShapeInfo = null;
        boolean isGoodOpen = true;
        int color = 0;
        //用道具随机出石头
        if(goodsInfo != null){
            if(goodsInfo.getType() != eGoodsType.VehicleSoulItem.getValue()){
                return null;
            }
            //第一个参数是随机颜色，就是职业，0就是所有职业同等概率随机，具体数值就是所有都是那个职业的
            //后面四个参数是品质对应的概率权重
            //假如配的不是同色的，有两个以上的随机到了同色，还要再随机找一块在随机一次看看是不是同色VEHICLES_SOUL_PURE_CHANGE_PRO
            List<BigInteger> pList = goodsInfo.getParamList();
            Map<Integer,Integer> qualityWeightMap = new ConcurrentHashMap<>();
            //凭证
            for(int quality = 1 ; quality < pList.size() ; quality++){
                if(pList.get(quality).intValue() != 0){
                    qualityWeightMap.put(quality,pList.get(quality).intValue());
                }
            }
            soulShapeInfo = VehicleSoulMgr.randomOneShapeByWeight(qualityWeightMap);
            color = pList.get(0).intValue();
        }else{
            isGoodOpen = false;
            soulShapeInfo = VehicleSoulMgr.randomOneShapeByQuality(makeQuality);
        }

        if(soulShapeInfo != null){
            UserVehicleSoul userVehicleSoul = new UserVehicleSoul();
            userVehicleSoul.setUserId(player.getUserId());
            userVehicleSoul.setSoulId(soulIdIndex.getAndIncrement());
            userVehicleSoul.setConfigId(soulShapeInfo.getId());
            userVehicleSoul.setVehicleId("");
            userVehicleSoul.setEquipTime(0);
            userVehicleSoul.setEquipPos(0);
            userVehicleSoul.setInsertOption();
            //每个砖块的职业和副词条加资质
            userVehicleSoul.setOccList(VehicleSoulMgr.randomOneShapeOccList(color,soulShapeInfo.getNum(),soulShapeInfo.getQuality() == 4));
            userVehicleSoul.setEntryMap(VehicleSoulMgr.randomOneShapeEntryList(soulShapeInfo));

            if(save){
                userVehicleSoulMap.put(userVehicleSoul.getSoulId(),userVehicleSoul);
                List<UserVehicleSoul> syncList = new ArrayList<>();
                syncList.add(userVehicleSoul);
                syncVehicleSoulData(syncList, false);
                AutoLogMgr.add(new LogVehicleSoulGet(getUserId(), 1, userVehicleSoul.getSoulId(), userVehicleSoul.getConfigId(),
                        userVehicleSoul.getOccList().size(),
                        StringUtils.listToString(userVehicleSoul.getOccList(), "|"),
                        VehicleSoulMgr.getSoulAllQaStr(userVehicleSoul), isGoodOpen ? 1 : 2));
            }
            if(goodsInfo != null){
                AttributeModule attributeModule = player.getModule(AttributeModule.class);
                long value = attributeModule.getAttribute(ePlayerAttrType.VehicleSoulOneKeyCompose);
                attributeModule.setAttribute(ePlayerAttrType.VehicleSoulOneKeyCompose,value + 1);
            }
            return userVehicleSoul;
        }

        return null;
    }



    @Override
    public boolean saveData() {
        if (null != userVehicleSoulMap) {
            for (UserVehicleSoul userVehicleSoul : userVehicleSoulMap.values()) {
                if (userVehicleSoul.isUpdateOption()) {
                    UserVehicleSoulBussiness.updateUserVehicleSoul(userVehicleSoul);
                } else if (userVehicleSoul.isInsertOption()) {
                    UserVehicleSoulBussiness.addUserVehicleSoul(userVehicleSoul);
                }
            }
        }
        return true;
    }


    @Override
    public boolean loadData() {
        soulIdIndex = new AtomicInteger(UserVehicleSoulBussiness.getUserVehicleSoulMaxId(getUserId()));
        userVehicleSoulMap = UserVehicleSoulBussiness.getUserVehicleSoulMap(getUserId());
        Map<String,Map<Integer,UserVehicleSoul>> tempEquipMap = new ConcurrentHashMap<>();
        //缓存一下每个门客装备了啥
        for(UserVehicleSoul userVehicleSoul : userVehicleSoulMap.values()){
            if(!StringUtils.isNullOrEmpty(userVehicleSoul.getVehicleId())){
                if(!tempEquipMap.containsKey(userVehicleSoul.getVehicleId())){
                    tempEquipMap.put(userVehicleSoul.getVehicleId(),new ConcurrentHashMap<>());
                }
                tempEquipMap.get(userVehicleSoul.getVehicleId()).put(userVehicleSoul.getSoulId(),userVehicleSoul);
            }
        }
        equipMap = tempEquipMap;
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public void loginSendMsg() {
        make5Soul();
        List<UserVehicleSoul> list = new ArrayList<>();
        for(UserVehicleSoul userVehicleSoul : userVehicleSoulMap.values()){
            if(!userVehicleSoul.isDelete()){
                list.add(userVehicleSoul);
            }
        }
        syncVehicleSoulData(list, true);
    }


    @Override
    public void afterLogin() {

    }

    private boolean isOnekeyCompose(){
        long count = player.getModule(AttributeModule.class).getAttribute(ePlayerAttrType.VehicleSoulOneKeyCompose);
        return count >= GameConfig.VEHICLES_SOUL_COMPOSE_ONEKEY;
    }


    private void syncVehicleSoulData(List<UserVehicleSoul> vehicleSoulList, boolean isLogin) {
        if(userVehicleSoulMap.size() == 0){
            return;
        }
        long composeBD = VehicleSoulMgr.VEHICLES_PEERLESS_GUARANTEES - player.getModule(AttributeModule.class).getAttribute(ePlayerAttrType.SoulWuShuang);
        if (isLogin) {
            if (vehicleSoulList.size() > VehicleSoulMgr.ONCE_SEND_VEHICLE_SOUL_COUNT) {//数量太多，分次下发
                do {
                    int count = vehicleSoulList.size();
                    if (count > VehicleSoulMgr.ONCE_SEND_VEHICLE_SOUL_COUNT) {
                        count = VehicleSoulMgr.ONCE_SEND_VEHICLE_SOUL_COUNT;
                    }
                    List<UserVehicleSoul> tempList = new ArrayList<>(vehicleSoulList.subList(0, count));
                    player.sendPacket(Protocol.U_VSOUL_SYNC, VehicleSoulPb.parseVehicleSoulSyncMsg(tempList,isOnekeyCompose(), (int) composeBD));
                    vehicleSoulList.removeAll(tempList);
                } while (vehicleSoulList.size() > 0);
            } else {
                player.sendPacket(Protocol.U_VSOUL_SYNC, VehicleSoulPb.parseVehicleSoulSyncMsg(vehicleSoulList,isOnekeyCompose(), (int) composeBD));
            }
        } else {
            player.sendPacket(Protocol.U_VSOUL_SYNC, VehicleSoulPb.parseVehicleSoulSyncMsg(vehicleSoulList,isOnekeyCompose(), (int) composeBD));
        }
    }

    public Map<Integer, UserVehicleSoul> getUserVehicleSoulMap() {
        Map<Integer, UserVehicleSoul> map = new ConcurrentHashMap<>();
        for(UserVehicleSoul userVehicleSoul : userVehicleSoulMap.values()){
            if(!userVehicleSoul.isDelete() && !StringUtils.isNullOrEmpty(userVehicleSoul.getVehicleId())){
                map.put(userVehicleSoul.getSoulId(),userVehicleSoul);
            }
        }
        return map;
    }

    public int soulReset(String vehicleUid) {
        UserVehicle userVehicle = player.getModule(VehicleModule.class).getUserVehicle(vehicleUid);
        if (userVehicle == null) {
            return GameErrorCode.E_VEHICLE_NO_FOUND;
        }

        long lastResetTime = player.getModule(AttributeModule.class).getAttribute(ePlayerAttrType.VehicleSoulLastResetTime);
        long currentTime = DateHelper.getCurrentTime();
        long passTime = currentTime - lastResetTime;
        if (passTime < GameConfig.VEHICLES_SOUL_RESET_CD) {
            return GameErrorCode.E_VEHICLE_SOUL_RESET_CD;
        }

        Property reward = new Property();
        reward.addProperty(VehicleSoulMgr.getSoulBlockConsume(userVehicle.getSoulBlockList().size()));
        if (reward.isNothing()) {
            return GameErrorCode.E_VEHICLE_SOUL_NOT_UNLOCK;
        }

        player.getModule(AttributeModule.class).setAttribute(ePlayerAttrType.VehicleSoulLastResetTime, currentTime);
        unloadSoul(vehicleUid);
        List<Integer> initSoulBlockList = StringUtils.stringToIntegerList(GameConfig.VEHICLES_SOUL_BEGIN_NUM, "\\|");
        userVehicle.setSoulBlockList(initSoulBlockList);

        player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.VehicleSoul, eLogMoneyType.VehicleSoulResetReward);

        VehicleProto.VehicleResetRespMsg.Builder respMsg = VehicleProto.VehicleResetRespMsg.newBuilder();
        respMsg.setRewards(PropertyHelper.parsePropertyToString(reward));
        respMsg.setRet(0);
        respMsg.setVehicle(VehiclePb.parseVehicleTempMsg(userVehicle));
        player.sendPacket(Protocol.S_VEHICLE_SOUL_RESET%20000, respMsg);

        return 0;
    }



}
